package com.clothes.act;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.clothes.R;
import com.clothes.app.BaseUrl;
import com.clothes.http.JsonCallBack;
import com.clothes.mdoel.Clothes;
import com.clothes.mdoel.ClothesInfo;
import com.clothes.mdoel.ServerInfo;
import com.clothes.utils.DateTool;
import com.clothes.utils.StringUtils;
import com.clothes.views.AddLinearLayout;
import com.clothes.views.MyHorizontalScrollView;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 载片登记
 */
public class SlideRegister extends BaseActivity {

    @Bind(R.id.et_size)
    EditText etSize;
    @Bind(R.id.et_color)
    EditText etColor;
    @Bind(R.id.ll_size)
    AddLinearLayout llSize;
    @Bind(R.id.ll_color)
    AddLinearLayout llColor;
    @Bind(R.id.ll_color_num)
    AddLinearLayout llColorNum;
    @Bind(R.id.hl_color)
    MyHorizontalScrollView hlColor;
    @Bind(R.id.hl_color_num)
    MyHorizontalScrollView hlColorNum;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.tv_all)
    TextView tvAll;
    @Bind(R.id.et_kh)
    EditText etKh;
    @Bind(R.id.iv_old)
    ImageView ivOld;

    //老款
    private List<String> sizeList = new ArrayList<>();
    private List<String> colorList = new ArrayList<>();
    private String uuid; //老款的uuid


    @Override
    public void initView() {
        setContentView(R.layout.activity_slide_register);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void initData() {
        tvTitle.setText("载床数量登记");
        tvTime.setText(DateTool.getStringDate(System.currentTimeMillis(),"yyyy-MM-dd HH:mm"));
        setTime(5);
        llSize.setName("尺码");
        llSize.addView(1,null);
        llColor.setName("颜色");
        llColor.addView(1,null);
        llColorNum.setName("数量");
        llColorNum.addView(1,null);


        hlColor.setOnScrollChanged(new MyHorizontalScrollView.OnScrollChanged() {
            @Override
            public void onScroll(int i, int i1, int i2, int i3) {
                hlColorNum.scrollTo(i, i1);
            }
        });
        hlColorNum.setOnScrollChanged(new MyHorizontalScrollView.OnScrollChanged() {
            @Override
            public void onScroll(int i, int i1, int i2, int i3) {
                hlColor.scrollTo(i, i1);
            }
        });


        etSize.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(ivOld.getVisibility() == View.VISIBLE) return;
                String txt = etSize.getText().toString();
                if(txt.length()>0 && parseInt(txt) > 6){
                    etSize.setText("6");
                    etSize.setSelection(txt.length());
                }
                int num = parseInt(etSize.getText().toString());
                llSize.addView(num,null);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        etColor.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(ivOld.getVisibility() == View.VISIBLE) return;
                String txt = etColor.getText().toString();
                if(txt.length()>0 && parseInt(txt) > 8){
                    etColor.setText("8");
                    etColor.setSelection(txt.length());
                }
                int num = parseInt(etColor.getText().toString());
                llColor.addView(num,null);
                llColorNum.addView(num,null);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        llColorNum.setOnTextChangeListener(new AddLinearLayout.onTextChangeListener() {
            @Override
            public void txtChange(String txt) {
                // TODO: 2018/4/24 更新颜色数量总数
                changeNum();
            }
        });

        etKh.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String res = s.toString();
                if(res.length() > 3){
                    getDataByKh(res);
                }else {
                    if(res.length() == 3){
                        setError();
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void setTimeText(String time) {
        tvTime.setText(time);
    }

    @OnClick({R.id.iv_back, R.id.tv_time, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.tv_time:
                timePickerView.show();
                break;
            case R.id.btn_submit:
                submit();
                break;
        }
    }

    //提交
    private void submit(){
        String kh = etKh.getText().toString();
        String size = etSize.getText().toString();
        String color = etColor.getText().toString();
        if(kh.equals("")){
            showToast("请输入款号");
            return;
        }
        if(kh.length() < 4){
            showToast("请输入4位以上款号");
            return;
        }
        if(size.equals("")){
            showToast("请输入尺寸数量");
            return;
        }
        if(color.equals("")){
            showToast("请输入颜色数量");
            return;
        }
        if(llSize.isEmpty()){
            showToast("尺寸名称不能为空");
            return;
        }
        if(llColor.isEmpty()){
            showToast("颜色名称不能为空");
            return;
        }
        if(llColorNum.isEmpty()){
            showToast("颜色数量不能为空");
            return;
        }
        //检验 颜色是否有重复的
        if(llColor.isCopy()){
            showToast("颜色名称有相同的");
            return;
        }
        //检验 尺寸是否有重复的
        if(llSize.isCopy()){
            showToast("尺寸名称有相同的");
            return;
        }
        Clothes clothes = new Clothes();
        clothes.setDate(tvTime.getText().toString());
        clothes.setModelNumber(kh);
        clothes.setSizeStyle(parseInt(size));
        clothes.setSize(StringUtils.getStrByList(llSize.getList()));
        clothes.setColorStyle(parseInt(color));
        clothes.setColors(StringUtils.getStrByList(llColor.getList()));
        clothes.setColorNumber(StringUtils.getStrByList(llColorNum.getList()));
        if(ivOld.getVisibility() == View.VISIBLE) //老款提交数据
            clothes.setUuid(uuid);
        final String json = new Gson().toJson(clothes);
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(ivOld.getVisibility() == View.GONE) //非老款提交数据
                    post(json,BaseUrl.CLOTHES);
                else //新款修改数据
                    put(json,BaseUrl.CLOTHES);
            }
        }).start();

    }

    @Override
    protected void onSuccess(String res) {
        showToast("提交成功");
        onBackPressed();
    }

    //更新数量
    private void changeNum(){
        int all = 0;
        for (String ss:llColorNum.getList()){
            all += parseInt(ss);
        }
        tvAll.setText(all+"件");
    }

    //根据款号请求
    private void getDataByKh(String khNo) {
        OkGo.<ServerInfo<List<ClothesInfo>>>get(BaseUrl.LH1).params("modelNumber", khNo)
                .execute(new JsonCallBack<ServerInfo<List<ClothesInfo>>>() {
                    @Override
                    public void onSuccess(Response<ServerInfo<List<ClothesInfo>>> response) {
                        ServerInfo serverInfo = response.body();
                        List<ClothesInfo> list = (List<ClothesInfo>) serverInfo.getData();
                        if (list.size() > 0) { //老款
                            ivOld.setVisibility(View.VISIBLE);
                            ClothesInfo info = list.get(0);
                            colorList = StringUtils.getListByStr(info.getColors(), ",");
                            sizeList = StringUtils.getListByStr(info.getSize(), ",");
                            uuid = info.getUuid();

                            llSize.addView(sizeList.size(),sizeList);
                            llColor.addView(colorList.size(),colorList);
                            llColorNum.addView(colorList.size(),null);
                            etSize.setText(sizeList.size()+"");
                            etSize.setEnabled(false);
                            etColor.setText(colorList.size()+"");
                            etColor.setEnabled(false);
                            changeNum();
//                            colorNums = StringUtils.getListByStr(info.getColorNumber(), ",");
                        }else {
                            setError();
                        }
                    }

                    @Override
                    public void onError(Response<ServerInfo<List<ClothesInfo>>> response) {
                        showToast("登记信息查询失败");
                        setError();
                    }
                });
    }

    //设置款号不对或者请求错误
    private void setError(){
        //错误 或者不是老款
        ivOld.setVisibility(View.GONE);
        etColor.setEnabled(true);
        etSize.setEnabled(true);
        etSize.setText("1");
        etColor.setText("1");
        llSize.addView(1,null);
        llColor.addView(1,null);
        llColorNum.addView(1,null);
        changeNum();
    }
}
